﻿using Apewer;
using System;
using System.Collections.Generic;
using System.Text;

namespace Apewer.Internals
{

    internal class TextVerifier
    {

        /// <summary>验证符串在父字符串中出现的次数。</summary>
        /// <param name="argParent">父字符串。</param>
        /// <param name="argSub">子字符串。</param>
        /// <param name="argIgnoreCase">是否忽略大小写。</param>
        public static int Exists(string argParent, string argSub, bool argIgnoreCase = false)
        {
            if (string.IsNullOrEmpty(argParent) || string.IsNullOrEmpty(argSub)) return 0;
            string vp = argParent;
            string vs = argSub;
            int vr = 0;
            int vl;
            int vi;
            try
            {
                if (argIgnoreCase) { vp = TextHelper.LCase(vp); vs = TextHelper.LCase(vs); }
                vl = vs.Length;
                vi = 1;
                while (vi <= (vp.Length - vl + 1))
                {
                    if (TextHelper.Mid(vp, vi, vl) == vs) { vr += 1; vi += vl; }
                    else vi += 1;
                }
                return vr;
            }
            catch { return 0; }
        }

        /// <summary>验证字符或字符串是否全部存在于字符池，默认选项等效于判断 (exists > 0) 是否为 True。</summary>
        /// <param name="argPool">字符池，允许出现的字符组合。</param>
        /// <param name="argSub">要查找的字符或字符串。</param>
        /// <returns>若 _sub 不存在于 _parent，则返回 False。</returns>
        public static bool Include(string argPool, string argSub)
        {
            return Include(argPool, argSub, false, false);
        }

        /// <summary>验证字符或字符串是否全部存在于字符池，默认选项等效于判断 (exists > 0) 是否为 True。</summary>
        /// <param name="argPool">字符池，允许出现的字符组合。</param>
        /// <param name="argSub">要查找的字符或字符串。</param>
        /// <param name="argAlone">分别验证 _sub 里的每个字符，若为 False 则验证 _sub 整体。</param>
        /// <returns>若 _sub 或 _sub 中任何一个字符不存在于 _parent，则返回 False。</returns>
        public static bool Include(string argPool, string argSub, bool argAlone)
        {
            return Include(argPool, argSub, argAlone, false);
        }

        /// <summary>验证字符或字符串是否全部存在于字符池，默认选项等效于判断 (exists > 0) 是否为 True。</summary>
        /// <param name="argPool">字符池，允许出现的字符组合。</param>
        /// <param name="argSub">要查找的字符或字符串。</param>
        /// <param name="argAlone">分别验证 _sub 里的每个字符，若为 False 则验证 _sub 整体。</param>
        /// <param name="argIgnoreCase">是否忽略大小写。</param>
        /// <returns>若 _sub 或 _sub 中任何一个字符不存在于 _parent，则返回 False。</returns>
        public static bool Include(string argPool, string argSub, bool argAlone = false, bool argIgnoreCase = false)
        {
            //{
            //    bool vnp = string.IsNullOrEmpty(argpool);
            //    bool vns = string.IsNullOrEmpty(argsub);
            //    if (vnp && vns) return true;
            //    if ((!vnp) && vns) return true;
            //    if (vnp && (!vns)) return false;
            //}
            ////try
            //{
            //    string vp = argpool;
            //    string vs = argsub;
            //    if (argignorecase) { vp = vp.ToLower(); vs = vs.ToLower(); }
            //    if (argalone)
            //    {
            //        string vc;
            //        int vl = 0;
            //        for (int vi = 1; vi <= vs.Length; vi++)
            //        {
            //            vc = Mid(vs, vi, 1);
            //            int vinstr = InStr(vp, vc);
            //            if (InStr(vp, vc) > 0) vl += 1;
            //        }
            //        if (vl == vs.Length) return true;
            //    }
            //    else
            //    {
            //        if (InStr(vp, vs) > 0) return true;
            //    }
            //    return false;
            //}
            ////catch
            ////{
            ////    return false;
            ////}

            bool vnp = string.IsNullOrEmpty(argPool);
            bool vns = string.IsNullOrEmpty(argSub);
            if (vnp && vns) return true;
            if ((!vnp) && vns) return true;
            if (vnp && (!vns)) return false;
            //try
            {
                string vp = string.IsNullOrEmpty(argPool) ? "" : argPool;
                string vs = string.IsNullOrEmpty(argSub) ? "" : argSub;
                if (argIgnoreCase) { vp = vp.ToLower(); vs = vs.ToLower(); }
                if (argAlone)
                {
                    var vcount = 0;
                    for (int vi = 0; vi < vs.Length; vi++)
                    {
                        if (vp.Contains(vs[vi].ToString())) vcount += 1; else break;
                    }
                    return vcount == vs.Length;
                }
                else
                {
                    return argPool.Contains(argSub);
                }
            }
            //catch
            //{
            //    return false;
            //}
        }

        /// <summary>验证字符串内容是否为 IP v4 地址，格式为“0.0.0.0”。</summary>
        public static bool IsIP(string argIp)
        {
            if (string.IsNullOrEmpty(argIp)) return false;
            try
            {
                string[] va;
                va = TextUtility.Split(argIp, '.');
                if (va.Length != 4) return false;
                for (int i = 0; i <= 3; i++)
                {
                    if (!IsInteger(va[i])) return false;
                    int vint = System.Convert.ToInt32(va[i]);
                    if (vint < 0) return false;
                    if (vint > 255) return false;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }


        ///<summary>验证字符串内容是否为数字，包含整数和小数。</summary>
        public static bool IsNumber(string argText)
        {
            if (IsInteger(argText)) return true;
            if (IsDecimal(argText)) return true;
            return false;
        }

        /// <summary>验证字符串内容是否为整数。</summary>
        public static bool IsInteger(string argText)
        {
            //try
            {
                if (string.IsNullOrEmpty(argText)) return false;
                string vtext = argText.Trim();
                if (string.IsNullOrEmpty(vtext)) return false;
                if (vtext.StartsWith("-")) vtext = TextHelper.Mid(vtext, 2, TextHelper.Len(vtext) - 1);
                return Include(Constant.NumberCollection, vtext, true);
            }
            // catch { return false; }
        }

        /// <summary>验证字符串内容是否为小数，格式：0.0。</summary>
        public static bool IsDecimal(string argText)
        {
            // try
            {
                string vtext = argText.Trim();
                if (string.IsNullOrEmpty(vtext)) return false;
                string[] va;
                va = TextUtility.Split(vtext, '.');
                if (va.Length == 2)
                {
                    for (int vi = 0; vi <= 1; vi++) if (!IsInteger(va[vi])) return false;
                    return true;
                }
                return false;
            }
            // catch { return false; }
        }

        /// <summary>验证字符串内所有字符是否均为十六进制字符。</summary>
        public static bool IsHex(string argText)
        {
            return Include(Constant.HexCollection, argText, true, true);
        }

        /// <summary>验证字符串内所有字符是否均为字母。</summary>
        public static bool IsLetter(string argText)
        {
            if (string.IsNullOrEmpty(argText)) return false;
            return Include(Constant.LetterCollection, argText, true, false);
        }

        /// <summary>验证字符串内所有字符是否均为大写字母。</summary>
        public static bool IsUpper(string argText)
        {
            if (string.IsNullOrEmpty(argText)) return false;
            return Include(Constant.UpperCollection, argText, true, true);
        }

        /// <summary>验证字符串内所有字符是否均为小写字母。</summary>
        public static bool IsLower(string argText)
        {
            if (string.IsNullOrEmpty(argText)) return false;
            return Include(Constant.LowerCollection, argText, true, true);
        }

        /// <summary>验证字符串内所有字符是否均为易识别字符。</summary>
        public static bool IsLucid(string argText)
        {
            if (string.IsNullOrEmpty(argText)) return false;
            return Include(Constant.LucidCollection, argText, true, true);
        }

    }

}
